﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Com.Charence.PassManager.UI.Web.Common
{
    public class JsonConverterAttribute : ActionFilterAttribute
    {
        public string ParameterName { get; set; }

        public Type ParameterType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string inputString;
            using (var reader = new StreamReader(filterContext.RequestContext.HttpContext.Request.InputStream))
            {
                inputString = reader.ReadToEnd();
            }
            var serializer = new JavaScriptSerializer();
            var parameter = serializer.Deserialize(inputString, ParameterType);
            filterContext.ActionParameters[ParameterName] = parameter;

            base.OnActionExecuting(filterContext);
        }
    }
}